// #ifdef APP-PLUS
import twVideon from '@/components/tsp-video/tsp-video-list/video-n.nvue'
import commentPopup from '@/components/tsp-video/comment-popup/comment-popup.nvue'
import shopPopup from '@/components/tsp-video/shop-popup/shop-popup.nvue'
import sharePopup from '@/components/tsp-video/share-popup/share-popup.nvue'
// #endif
import pageConfig from './pageConfig.js'
import {
	API_BASE_URL,
	SERIAL,
	IS_DEV,
	ACCEPT_PLATFORM,
	LOGIN_PAGE_URL
} from '@/common/config.js';
import nvueMixinfrom from '@/common/mixin/nvueMixin.js';
export default {
	mixins: [nvueMixinfrom],
	// #ifdef APP-PLUS
	components: {
		twVideon,
		shopPopup,
		commentPopup,
		sharePopup
	},
	// #endif
	data() {
		return {
			isTabbar: true, //是否为tabbar 页面
			show: false,
			flag: false,
			pageNum: 1,
			last_page: 1,
			is_recommend: '',
			grassclassify_id: '',
			grasstopic_id: '',
			keywords: '',
			sort: '',
			is_follow: '',
			city: '',
			city_id: '',
			address_code_city: '',
			user_id: '',
			aid: '',
			videoData: [],
			nextPlay: true,
			activeData: {},
			activeIndex: 0,
			videoListArr: []
		}
	},
	onLoad({
		is_recommend,
		grassclassify_id,
		grasstopic_id,
		keywords,
		sort,
		is_follow,
		city,
		city_id,
		address_code_city,
		user_id,
		aid
	}) {
		if (is_recommend !== undefined) this.is_recommend = is_recommend
		if (grassclassify_id !== undefined) this.grassclassify_id = grassclassify_id
		if (grasstopic_id !== undefined) this.grasstopic_id = grasstopic_id
		if (keywords !== undefined) this.keywords = keywords
		if (sort !== undefined) this.sort = sort
		if (is_follow !== undefined) this.is_follow = is_follow
		if (city !== undefined) this.city = city
		if (city_id !== undefined) this.city_id = city_id
		if (address_code_city !== undefined) this.address_code_city = address_code_city
		if (user_id !== undefined) this.user_id = user_id
		if (aid !== undefined) this.aid = aid
		uni.$on(pageConfig.interaction.updateAttentionStatus, this.updateAttentionStatus);
		uni.$on(pageConfig.interaction.updateVideoRemove, (data) => {
			this.$refs.videoGroup.filterVodList(data, 'del')
		});
		uni.$on(pageConfig.interaction.updateVideoBlack, (data) => {
			this.$refs.videoGroup.filterVodList(data, 'black')
		});
	},
	onReady(options) {
		this.getData(true)
	},
	onShow() {
		console.log('1111111111111');
		/* 播放视频 */
		if (this.$refs.videoGroup) this.$refs.videoGroup.showPlay()
	},
	onHide() {
		/* 暂停视频 */
		if (this.$refs.videoGroup) this.$refs.videoGroup.hidePause()
	},
	computed: {
		isLogin() {
			return this.$store.state.token == '' ? false : true
		},
		userInfo: {
			get() {
				return this.$store.state.userInfo
			},
			set() {
				this.$store.commit('updateUserInfo', value)
			},
		},
	},
	onShareAppMessage({
		from
	}) {
		let activeData = {}
		if (from == "button") {
			activeData = this.activeData
		} else {
			activeData = this.$refs.videoGroup.totalPlayList[this.$refs.videoGroup.vodCurIndex]
		}
		return {
			title: activeData.title,
			path: `${pageConfig.interaction.wxShareUrl}?aid=${activeData.aid}&share_user_id=${this.userInfo.id}`,
			imageUrl: activeData.coverImg
		}
	},
	methods: {
		//记录视频浏览量
		async get_course_chapterApi(val) {
			const res = await this.http(pageConfig.API.set_videoplaylog, 'POST', {
				shortvideo_id: this.videoListArr[val].aid
			})

			console.log(res);
		},

		updateAttentionStatus(data) {
			// 判断 data.followReally是否为boolean类型
			if (typeof data.followReally != 'boolean') {
				data.followReally == 1 ? true : false //是否点赞
			}
			this.$refs.videoGroup.updateAttentionStatus(data)
		},
		handleGoods(item) {
			console.log('点击商品', item)
			pageConfig.interaction.handleGoods(item)
		},
		closePopup() {
			this.nextPlay = true
		},
		handleMenu({
			type,
			index,
			data
		}) {
			if (!this.isLogin) return uni.navigateTo({
				url: LOGIN_PAGE_URL
			})
			switch (type) {
				case 1:
					console.log('点击头像')
					if (data.is_self) {
						pageConfig.interaction.handleSelfAvatar(data)
					} else {
						pageConfig.interaction.handleAvatar(data)
					}
					break
				case 2:
					console.log('点击关注')
					this.setGrassFollow(index, data)
					break
				case 3:
					console.log('点击点赞')
					this.addGrassLike(index, data)
					break
				case 4:
					console.log('点击评论')
					this.nextPlay = false
					this.activeData = data
					this.activeIndex = index
					this.commentPage = 1
					this.getGrassCommentList()
					break
				case 5:
					console.log('点击收藏')
					this.addGrassCollection(index, data)
					break
				case 6:
					console.log('点击转发')
					this.nextPlay = false
					this.activeData = {
						...data,
						miniProgram: {
							id: pageConfig.interaction.wxAppId,
							wxShareUrl: `${pageConfig.interaction.wxShareUrl}?aid=${data.aid}&share_user_id=${this.userInfo.id}`,
							webUrl: `${pageConfig.interaction.appShareUrl}?aid=${data.aid}&share_user_id=${this.userInfo.id}`
						},
						wxShareUrl: `${pageConfig.interaction.wxShareUrl}?aid=${data.aid}&share_user_id=${this.userInfo.id}`,
						appShareUrl: `${pageConfig.interaction.appShareUrl}?aid=${data.aid}&share_user_id=${this.userInfo.id}`,
						codeShareUrl: `${pageConfig.interaction.codeShareUrl}?aid=${data.aid}&share_user_id=${this.userInfo.id}`,
						shareType: pageConfig.interaction.shareType,
					}
					this.$refs.sharePopup.open()
					break
				case 7:
					console.log('旋转头像')
					break
				case 8:
					console.log('点击店铺')
					this.nextPlay = false
					this.activeData = data
					this.activeIndex = index
					this.$refs.shopPopup.openPopup()
					break
				case 9:
					console.log('点击话题')
					pageConfig.interaction.handleGrasstopic(data)
					break
				case 'block':
					console.log('点击拉黑')
					this.editBlacklist(data)
					break
				case 'feedback':
					console.log('点击举报')
					pageConfig.interaction.handleFeedback(data)
					break
				case 'delet':
					console.log('点击删除')
					uni.showModal({
						title: '提示',
						content: this.$t('确认删除吗?'),
						showCancel: true,
						success: ({
							confirm,
							cancel
						}) => {
							if (confirm) {
								this.delArticle(data)
							} else if (cancel) {
								return false
							}
						}
					})
					break
				case 'edit':
					console.log('点击编辑')
					pageConfig.interaction.handleEdit(data)
					break
			}
		},
		async editBlacklist(postData) {
			try {
				let {
					code,
					data,
					msg
				} = await this.http(pageConfig.API.editBlacklist, 'POST', {
					forbidden_user_id: postData.user_id,
				})
				if (code != 1) return this.$message.error(msg)
				this.$message.success(this.$t('已拉黑'))
				this.$refs.videoGroup.filterVodList(postData, 'black')
				uni.$emit(pageConfig.interaction.emitBlack, {
					user_id: videoInfo.user_id,
					aid: postData.aid
				})

			} catch (error) {
				console.log('error', error);
			}
		},
		async delArticle(postData) {
			try {
				let {
					code,
					data,
					msg
				} = await this.http(pageConfig.API.delArticle, 'POST', {
					aid: postData.aid,
				})
				if (code != 1) return this.$message.error(msg)
				this.$message.success(this.$t('删除成功'))
				this.$refs.videoGroup.filterVodList(postData, 'del')
			} catch (error) {
				console.log('error', error);
			}
		},
		async sendMsg(data) {
			try {
				if (this.flag) return
				this.flag = true
				let res = null
				if (!data.isReply) {
					res = await this.http(pageConfig.API.addGrassComment, 'POST', {
						grass_id: this.activeData.aid,
						content: data.sendMsg
					})
				} else {
					res = await this.http(pageConfig.API.addGrassCommentReply, 'POST', {
						grass_id: this.activeData.aid,
						grasscomment_id: data.aid ? data.aid : '',
						content: data.sendMsg
					})
				}
				this.flag = false
				if (res.code != 1) return uni.showToast({
					title: res.msg,
					icon: 'none',
				})
				if (!data.isReply) {
					this.activeData.discussNum++
					this.$refs.videoGroup.updateData(this.activeIndex, 'discussNum', this.activeData.discussNum)
				}
				this.commentPage = 1
				uni.$emit(pageConfig.interaction.emitComment, {
					aid: this.activeData.aid,
					num: this.activeData.discussNum
				})
				this.getGrassCommentList()
			} catch (error) {
				this.flag = false
			}
		},
		// 评论列表
		async getGrassCommentList() {
			if (this.flag) return
			this.flag = true
			try {
				let {
					data: {
						data,
						last_page,
						current_page
					},
					code,
					msg
				} = await this.http(pageConfig.API.getGrassCommentList, 'POST', {
					grass_id: this.activeData.aid,
					page: this.commentPage,
					list_rows: 15,
				})
				this.flag = false
				if (code != 1) return uni.showToast({
					title: msg,
					icon: 'none',
				})
				this.commentLastPage = last_page
				if (this.commentPage == 1) {
					this.$refs.commentPopup.initList(data)
				} else {
					this.$refs.commentPopup.lodingData(data)
				}
			} catch (error) {
				this.flag = false
			}
		},
		discussLoadData() {
			if (this.flag) return
			if (this.commentLastPage > this.commentPage) {
				this.commentPage++
				this.getGrassCommentList()
			}
		},
		// 添加关注
		async setGrassFollow(index, videoInfo) {
			try {
				if (this.flag) return
				this.flag = true
				let {
					data,
					code,
					msg
				} = await this.http(pageConfig.API.setGrassFollow, 'POST', {
					idol_user_id: videoInfo.user_id
				})
				this.flag = false
				if (code != 1) return uni.showToast({
					title: msg,
					icon: 'none',
				})
				this.updateAttentionStatus({
					user_id: videoInfo.user_id,
					followReally: !videoInfo.followReally
				})
				uni.$emit(pageConfig.interaction.emitStatus, {
					user_id: videoInfo.user_id,
					type: 1
				})
			} catch (error) {
				console.log('error', error);

				this.flag = false
			}
		},

		// 添加点赞
		async addGrassLike(index, videoInfo) {
			try {
				if (this.flag) return
				this.flag = true
				let {
					data,
					code,
					msg
				} = await this.http(pageConfig.API.addGrassLike, 'POST', {
					grass_id: videoInfo.aid
				})
				this.flag = false
				if (code != 1) return uni.showToast({
					title: msg,
					icon: 'none',
				})
				let num = videoInfo.fabulousNum
				videoInfo.fabulousShow ? num-- : num++
				uni.$emit(pageConfig.interaction.emitFabulous, {
					type: videoInfo.fabulousShow ? 0 : 1,
					aid: videoInfo.aid
				})
				this.$refs.videoGroup.updateData(index, 'fabulousShow', !videoInfo.fabulousShow)
				this.$refs.videoGroup.updateData(index, 'fabulousNum', num)
			} catch (error) {
				this.flag = false
			}
		},
		// 添加收藏
		async addGrassCollection(index, videoInfo) {
			try {
				if (this.flag) return
				this.flag = true
				let {
					data,
					code,
					msg
				} = await this.http(pageConfig.API.addGrassCollection, 'POST', {
					collect_id: videoInfo.aid,
					collect_type: videoInfo.collectShow ? 0 : 1
				})
				this.flag = false
				if (code != 1) return uni.showToast({
					title: msg,
					icon: 'none',
				})
				let num = videoInfo.collectNum
				videoInfo.collectShow ? num-- : num++
				uni.$emit(pageConfig.interaction.emitCollect, {
					type: videoInfo.collectShow ? 0 : 1,
					aid: videoInfo.aid
				})
				this.$refs.videoGroup.updateData(index, 'collectShow', !videoInfo.collectShow)
				this.$refs.videoGroup.updateData(index, 'collectNum', num)

			} catch (error) {
				this.flag = false
			}
		},
		http(url, type, data) {
			return new Promise((resolve, reject) => {
				uni.request({
					url: API_BASE_URL + url,
					dataType: 'json',
					responseType: 'text',
					header: {
						'Content-Type': 'application/json; charset=utf-8',
						'user-token': this.$store.state.token || '',
						// 'Accept-Language': this.$store.state.locale?.value,
						// 'Accept-Platform': ACCEPT_PLATFORM,
						// 'Accept-Serial': SERIAL,
					},
					method: type || "GET",
					data: data,
					success: (res) => resolve(res.data),
					fail: reject
				})
			})
		},
		async getData(type) {
			try {
				if (this.flag) return
				this.flag = true
				let {
					data: {
						data,

					},
					last_page,
					current_page,
					code,
					msg
				} = await this.http(pageConfig.API.getVideoList, 'POST', {
					is_recommend: this.is_recommend,
					grassclassify_id: this.grassclassify_id,
					grasstopic_id: this.grasstopic_id,
					keywords: this.keywords,
					sort: this.sort,
					is_follow: this.is_follow,
					city: this.city,
					city_id: this.city_id,
					address_code_city: this.address_code_city,
					user_id: this.user_id,
					aid: this.aid,
					page: this.pageNum,
					// list_rows: 15,
					// grass_type: 1
				})


				this.flag = false
				if (code != 1) return uni.showToast({
					title: msg,
					icon: 'none',
				})
				this.last_page = last_page

				console.log('2222222222', data, msg);
				let videos = data.map((item) => ({
					vodUrl: item.video,
					coverOpacity: '',
					title: item.shortvideo_title,
					avatar: item.avatar, //头像
					address: '',
					coverImg: item.fmt, //视频封面
					topic: '', //标签
					nickname: '', //昵称
					shopNum: 0, //是否显示商店item.grass_goods.length
					fabulousShow: false, //是否点赞
					fabulousNum: '', //点赞数
					collectShow: false, //是否收藏
					collectNum: '', //收藏数
					discussNum: '', //评论数
					coverShow: true, //是否显示视频封面，小程序端不播放会显示视频，可以不用显示封面，App不播放不会显示视频，就需要封面了
					object_fit: 'contain', //视频的显示类型 contain
					sliderShow: true, //是否显示进度条
					rotateImgShow: false, //是否显示旋转头像
					followReally: false, //是否关注
					...item
				}))

				if (!videos.length) return
				if (this.pageNum == 1) {
					type ? this.$refs.videoGroup.initVod(videos, 0) : this.$refs.videoGroup.refreshSquare(videos)
				} else {
					this.$refs.videoGroup.lodingData(videos)
				}

				//
				this.videoListArr = videos
			} catch (error) {
				this.flag = false
			}
		},
		/* 下拉刷新 */
		refreshData() {
			this.pageNum = 1
			this.getData()
		},
		/* 上拉加载 */
		loadingData() {
			if (this.flag) return
			if (this.last_page > this.pageNum) {
				this.pageNum++
				this.getData()
			}
		},
	},
}